#
import requests,re,json,pymysql
def index():
    req = requests.get("http://55.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112403397853268748843_1576835626335&pn=1&pz=2102&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:116+t:3&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152%271576835626336")
    req.encoding=req.apparent_encoding
    html=req.text
    #print(html)
    aa = re.compile("(\[{.+?}\])")
    result = aa.findall(html)
    #print(result)
    aaa = json.loads(result[0])
    #print(aaa)
    a= 0
    for i in aaa:
        print(i)
        c = [i['f12'],i['f14'],i['f2'],i['f4'],i['f24'],i['f17'],i['f15'],i['f16'],i['f18'],i['f5'],i['f6']]
        mysql_insert(c)
import redis
def mysql_insert(i):
    r = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True)
    if r.exists(i[1]):
        print("有数据")
    else:
        db = pymysql.connect("106.12.30.33","ykq","mbsb74194741","finance",charset="utf8")
        cursor = db.cursor()
        #sqlInsert = "DELETE FROM zhanglong"
        sqlInsert1 = "INSERT INTO zhanglong VALUES (0, '{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')".format(i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7],i[8],i[9],i[10])
        #print(sqlInsert)
        try:
            cursor.execute(sqlInsert1)
            #cursor.execute(sqlInsert1)
            db.commit()
            r.set(i[1],i[2])
            r.close()

        except:
            db.rollback()
            print("NO", cursor.rowcount)
        db.close()

index()